package com.zut.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncryptUtil {

    private static String publicKey;
    private static String privateKey;

    //生成公钥和私钥
    static{
        try {
            java.lang.String[] strings = ConfigTools.genKeyPair(512);
            //生成公钥
            publicKey = strings[1];
            //生成私钥
            privateKey = strings[0];

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }

    //私钥加明文密码生成加密密码
    public static String encrypt(String password) throws Exception {
        String encryptPassword = ConfigTools.encrypt(privateKey, password);
        return encryptPassword;
    }

    //加密密码加公钥解密出来明文密码
    public static String decrypt(String encryptPassword) throws Exception {
        String decryptPassword = ConfigTools.decrypt(publicKey, encryptPassword);
        return decryptPassword;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("加密密码为："+DruidEncryptUtil.encrypt("Cbx16637462201"));
        System.out.println("明文密码为："+DruidEncryptUtil.decrypt(DruidEncryptUtil.encrypt("Cbx16637462201")));
        System.out.println("publicKey:"+publicKey);

    }
}
